package com.example.prison.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.prison.entity.DeviceManagement;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author jiang
* @description 针对表【device_management】的数据库操作Mapper
* @createDate 2023-06-20 15:14:12
* @Entity com.example.military.entity.DeviceManagement
*/
@Mapper
public interface DeviceManagementMapper extends BaseMapper<DeviceManagement> {

    @Select("select * from device_management order by id desc")
    List<DeviceManagement> get();

    @Select("<script>select * from device_management " +
            " <where> " +
            " <if test=\"type != null and type != ''\">and type like concat('%',#{type},'%') </if> " +
            " <if test=\"deviceNumber != null and deviceNumber != ''\">and device_number like concat('%',#{deviceNumber},'%') </if> " +
            " </where> " +
            "order by id desc</script>")
    List<DeviceManagement> list(@Param("type") String type, @Param("deviceNumber") String deviceNumber);

    @Insert("insert into device_management(access,device_name, device_number, device_area, is_use, device_status,type) " +
            "values (#{access},#{deviceName}, #{deviceNumber}, #{deviceArea}, #{isUse}, #{deviceStatus}, #{type})")
    int add(DeviceManagement device);

    @Delete("<script>delete from device_management where id in <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item} </foreach> </script>")
    void deleteDevice(@Param("ids") Integer[] ids);

//    @Update("update device_management set device_name=#{device.deviceName},device_number=#{device.deviceNumber},
//    device_area=#{device.deviceArea},is_use=#{device.isUse},device_status=#{device.deviceStatus},type=#{device.type} where id = #{id}")
    @Update({"<script> UPDATE device_management",
            "<set>",
            "<if test='device.deviceName != null'>device_name = #{device.deviceName},</if>",
            "<if test='device.deviceNumber != null'>device_number = #{device.deviceNumber},</if>",
            "<if test='device.deviceArea != null'>device_area = #{device.deviceArea},</if>",
            "<if test='device.isUse != null'>is_use = #{device.isUse},</if>",
            "<if test='device.deviceStatus != null'>device_status = #{device.deviceStatus},</if>",
            "<if test='device.type != null'>type = #{device.type},</if>",
            "</set>",
            "where id = #{id} </script>"})
    int updateDevice(DeviceManagement device,Integer id);

    @Select("select * from device_management where id=#{id}")
    DeviceManagement getById(@Param("id") Integer id);

    @Select("select * from device_management where type like concat('%',#{type},'%') order by id desc")
    List<DeviceManagement> getByType(@Param("type") String type);

    @Select("select * from device_management where device_number like concat('%',#{deviceNumber},'%') order by id desc")
    List<DeviceManagement> getByNumber(@Param("deviceNumber") String deviceNumber);

    @Select("select * from device_management where device_number = #{deviceNumber}")
    List<DeviceManagement> getListOneByNumber(@Param("deviceNumber") String deviceNumber);

    @Select("select * from device_management where device_number=#{deviceNumber} order by id desc")
    DeviceManagement getOneByNumber(@Param("deviceNumber") String deviceNumber);

}




